function xup = automationup(sbar,stilde,xlow,p)

% compute maximal comparative advantage of capital relative to stilde:
xmax = fminbnd(@(x) loglaborprod(stilde,x,p)-logcapprod(x,p),0,1);

% compute xup:
if costcompup(sbar,stilde,xlow,xmax,p)<=1 % stilde is cheaper than capital at xmax
    
    xup = xmax;
    
else
    
    xup = fzero(@(x) costcompup(sbar,stilde,xlow,x,p)-1,[xmax 1-(1-stilde)*p.stol],p.optionsfzero);
    
end

end